package com.yuanhua.xcxmng.wechat.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlRunner;
import com.yuanhua.xcxmng.wechat.entity.User;
import com.yuanhua.xcxmng.wechat.entity.UserRun;
import com.yuanhua.xcxmng.wechat.entity.UserRunQuery;
import com.yuanhua.xcxmng.wechat.entity.UserTarget;
import com.yuanhua.xcxmng.wechat.mapper.UserMapper;
import com.yuanhua.xcxmng.wechat.mapper.UserRunMapper;
import com.yuanhua.xcxmng.wechat.mapper.UserTargetMapper;
import com.yuanhua.xcxmng.wechat.service.BaseService;
import com.yuanhua.xcxmng.wechat.service.MyService;
import com.yuanhua.xcxmng.wechat.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Ted(lyc32995)
 * @version : id: MyServiceImpl , v 0.1 2018/1/24 16:14 Ted(lyc32995) Exp $
 * @Description
 */
@Service
public class MyServiceImpl extends BaseService implements MyService {

    @Autowired
    private UserMapper       userMapper;
    @Autowired
    private UserRunMapper    userRunMapper;
    @Autowired
    private UserTargetMapper userTargetMapper;
    @Autowired
    private UserService      userService;

    @Override
    public void findAll() {

    }

    @Override
    public void saveRunData(String openId, Date gmtDate, Long step) {
        try {
            UserRun userRun = new UserRun();
            userRun.setGmtDate(gmtDate);
            userRun.setStep(step);
            userRun.setOpenId(openId);
            userRun.setCurrency(0L);
            userRun.setTargetyes(0L);
            userRunMapper.insert(userRun);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public UserRun findRunDataByOpenIdDate(String openId, Date date) {
        try {
            UserRun run = new UserRun();
            run.setOpenId(openId);
            run.setGmtDate(date);
            return userRunMapper.selectByOpenIdDate(run);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public List<UserRun> findRunDataByOpenIdDate(String openId, Date start, Date endDate) {
        try {
            UserRunQuery param = new UserRunQuery();
            param.setOpenId(openId);
            param.setStartDate(start);
            param.setEndDate(endDate);
            List<UserRun> userRuns = userRunMapper.selectByOpenIdDateInteval(param);
            return userRuns;
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public UserTarget findTargetByOpenId(String openId) {
        try {
            return userTargetMapper.selectByOpenId(openId);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void createTarget(UserTarget userTarget) {
        try {
            userTargetMapper.insert(userTarget);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void updateTarget(UserTarget userTarget) {
        try {
            userTargetMapper.updateByPrimaryKey(userTarget);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void createUser(User user) {
        try {
            userMapper.insert(user);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void updateUser(User user) throws Exception {
        try {
            userService.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void updateUserRun(UserRun userRun) {
        try {
            userRunMapper.updateByPrimaryKey(userRun);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public User findUserByOpenId(String openId) {
        try {
            return userMapper.findUserByOpenId(openId);
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public List<User> orderByStep() {
        try {
            return userMapper.orderByStep();
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public List<User> orderByNo() {
        try {
            return userMapper.orderByNo();
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }
}